//
//  StrategyModelDataSourceProtocolTests.swift
//  travelMapMvvm-swift2
//
//  Created by green on 15/11/3.
//  Copyright © 2015年 travelMapMvvm-swift2. All rights reserved.
//

import Foundation
import XCTest
import TMDatasourceLib
import TMCommonLib

class StrategyModelDataSourceProtocolTests: XCTestCase {
    
    var strategyModelDataSourceProtocol:StrategyModelDataSourceProtocol!
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
        
        strategyModelDataSourceProtocol = StrategyModelDataSource.shareInstance()
    }
    
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    /**
     系统推荐攻略测试
     */
    func testQueryStrategyListBySystem() {
        
        let expectation = expectationWithDescription(kURL + "GetArticleList")
        
        strategyModelDataSourceProtocol.queryStrategyListBySystem(1, pageSize: 10, sortCode: "1", strategyTypeCode: nil, strategyThemeCodes: ["G1","G2","G3"], monthCodes: ["1","2","3"]).subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(any is [StrategyModel],"response obj is not [StrategyModel]")
            
            print(any as! [StrategyModel])
            
        }, error: { (error:NSError!) -> Void in
        
            expectation.fulfill()
            XCTFail(error.localizedDescription)
            
        })
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })
    }
    
    /**
     关键字攻略查询测试
     */
    func testQueryStrategyListByKeyword() {
        
        let expectation = expectationWithDescription(kURL + "getarticlelistbykeyword")
        
        strategyModelDataSourceProtocol.queryStrategyListByKeyword(2, pageSize: 10, sortCode: "1", keyword: "东京").subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(any is [StrategyModel],"response obj is not [StrategyModel]")
            
            print(any as! [StrategyModel])
            
        }, error: { (error:NSError!) -> Void in
            
            expectation.fulfill()
            XCTFail(error.localizedDescription)
            
        })
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })
    }
    
    /**
     个人收藏攻略测试
     */
    func testQueryStrategyListByUserId() {
        
        let expectation = expectationWithDescription(kURL + "queryStrategyListByUserId")
        
        strategyModelDataSourceProtocol.queryStrategyListByUserId(1, pageSize: 10, sortCode: nil, userId: 1).subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(any is [StrategyModel],"response obj is not [StrategyModel]")
            
            print(any as! [StrategyModel])
            
        }, error: { (error:NSError!) -> Void in
            
            expectation.fulfill()
            XCTFail(error.localizedDescription)
            
        })
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })
    }
    
    /**
     收藏/取消攻略测试
     */
    func testCollectStrategy() {
        
        let expectation = expectationWithDescription(kURL + "setFavoriteArticle")
        
        strategyModelDataSourceProtocol.collectStrategy("3328778", userId: 4610549, isFavorite: false).subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(any is String, "response obj is not string type")
            
            let msg = any as? String
            
            print(msg)
            
        }) { (error:NSError!) -> Void in
            
            expectation.fulfill()
            XCTAssert(error.localizedDescription != DataError.CollectStrategyError(nil).error.localizedDescription, "collectStrategy failure")
        }
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })
    }
    
    /**
     点赞/取消攻略测试
     */
    func testLikeStrategy() {
        
        let expectation = expectationWithDescription(kURL + "likeStrategy")
        
        strategyModelDataSourceProtocol.likeStrategy("3328778", userId: 4610549, isLike: true).subscribeNext({ (any:AnyObject!) -> Void in
            
            expectation.fulfill()
            XCTAssert(any is String, "response obj is not string type")
            
            let msg = any as? String
            
            print(msg)
            
        }) { (error:NSError!) -> Void in
            
            expectation.fulfill()
            XCTAssert(error.localizedDescription != DataError.LikeStrategyError(nil).error.localizedDescription, "likeStrategy failure")
        }
        
        waitForExpectationsWithTimeout(10, handler: {error in
        })
    }
}